function setNewPageUrl() {
    /* Get parent page */
    var page;
    var objs;
    objs = document.getElementsByName("parentpage");
    for (var ii = 0; ii < objs.length; ii ++) {
        if (objs[ii].checked == true) {
            page = objs[ii].value;
            break;
        }
    }
    
    /* Get new page web address value */
    var newPageName = document.getElementById("newpage_address").value;
    
    var url;
    if (page != "toplevel") {
         url = window.location.pathname.trimEnd("/") + "/" + newPageName;
    }
    else {
        url = "/" + newPageName;
    }
    
    /* Set element values */
    document.getElementById("newpage_url").innerHTML = url;
    objs = document.getElementsByName("url");
    for (var ii = 0; ii < objs.length; ii ++) {
        objs[ii].value = window.location.protocol + "//" + window.location.host + url;
    }
}


function isValidWebPageAddress(inputText) {
    var patrn = /^[0-9A-Za-z\-]*$/;
    
    var newPageError = document.getElementById("newpage_address_error");
    
    if (patrn.exec(inputText)) {
        return true;
    }
    else {
        return false;
    }
}


function checkWebPageAddress() {
    var newPageAddressName = document.getElementById("newpage_address").value;
    var result = isValidWebPageAddress(newPageAddressName);
    
    var newPageError = document.getElementById("newpage_address_error");
    if (result == true) {
        newPageError.style.display="none";
        setNewPageUrl();
    }
    else {
        newPageError.style.display="inline";
    }
}

function validateSubmit() {
    var newPageAddressName = document.getElementById("newpage_address").value;
    if (newPageAddressName == null || newPageAddressName.length == 0) {
        alert("Please input the Page Web Address!");
        return false;
    }
    
    if (!isValidWebPageAddress(newPageAddressName)) {
        alert("Page Web Address contains invalid characters!");
        return false;
    }
    
    return true;
}
